package com.note.controller;

import com.note.pojo.*;
import com.note.service.ComLikeService;
import com.note.service.CommService;
import com.note.service.NoteService;
import com.note.service.UserService;
import com.note.vo.TComm;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/com")
public class CommController {
    @Autowired
    private CommService commService;
    @Autowired
    private ComLikeService comLikeService;
    @Autowired
    private NoteService noteService;
    @Autowired
    private UserService userService;

    //评论保存
    @RequestMapping("/save")
    public String save(Comm comm, HttpSession session){
        commService.inserComm(comm,session);
        return "redirect:/note/detail?id="+comm.getNoteid();
    }
    //评论点赞
    @ResponseBody
    @RequestMapping("/zan")
    public int zan(Integer comid, HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user!=null){
            ComLikes comLike = new ComLikes();
            comLike.setCommid(comid);
            comLike.setUserid(user.getId());
            ComLikes comLikes = comLikeService.getByUidCid(comLike);
            if (comLikes!=null){
                comLikes.setNum(comLikes.getNum()+1);
                comLikeService.addoneClikes(comLikes);
            }else {
                comLikeService.addClikes(comLike);
            }
        }else {
            ComLikes comLike = new ComLikes();
            comLike.setCommid(comid);
            comLike.setUserid(0);
            ComLikes comLikes = comLikeService.getByUidCid(comLike);
            if (comLikes!=null){
                comLikes.setNum(comLikes.getNum()+1);
                comLikeService.addoneClikes(comLikes);
            }else {
                comLikeService.addClikes(comLike);
            }
        }

        Comm comm = commService.getComById(comid);
        int likes = comm.getLikes() + 1;
        comm.setLikes(likes);
        commService.zan(comm);
        return likes;
    }
    //展示评论
    @RequestMapping("/listcoms")
    public String listcoms(Integer noteid, Model model){
        Note note = noteService.getNoteById(noteid);
//        int size =(int) Math.ceil((double) commService.getComsByNoteId(noteid).size()/8);
//        int[] sizes=new int[size];
//        for (int i = 0; i < sizes.length ; i++) {
//            sizes[i]=i+1;
//        }
//        PageHelper.startPage(pn,8);
        List<Comm> coms1 = commService.getComsByNoteId(noteid);
        List<TComm> coms = coms1.stream().map(comm1 -> {
            TComm comm = new TComm();
            BeanUtils.copyProperties(comm1,comm);
            comm.setId(comm1.getId());
            comm.setContents(comm1.getContents());
            comm.setCreatetime(comm1.getCreatetime());
            comm.setNoteid(comm1.getNoteid());
            comm.setLikes(comm1.getLikes());
            if (comm1.getUserid()==null){
                comm.setLoginname("游客");
                comm.setAvatar("upload/youke.jpg");
            }else {
                comm.setLoginname(userService.findNameById(comm1.getUserid()));
                comm.setAvatar(userService.findAvatarById(comm1.getUserid()));
            }
            return comm;
        }).collect(Collectors.toList());
//        PageInfo pageInfo=new PageInfo(coms,size);
//        pageInfo.setPageNum(pn);
//        pageInfo.setNavigatepageNums(sizes);
//        pageInfo.setPages(size);
        model.addAttribute("coms",coms);
        model.addAttribute("note",note);
        return "listcoms";
    }
    //删除评论
    @RequestMapping("/delComById")
    public String delComById(Integer id,Model model,Integer noteid){
        commService.delComById(id);
        return "redirect:/com/listcoms?noteid="+noteid;
    }
}
